package com.hengxinguotong.zhihuichengjian.ui;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.AualityTestAddPicAdapter;
import com.hengxinguotong.zhihuichengjian.adapter.BuildFileAdapter;
import com.hengxinguotong.zhihuichengjian.bean.FileBean;
import com.hengxinguotong.zhihuichengjian.bean.Message;
import com.hengxinguotong.zhihuichengjian.picturebrowser.view.TaskBigImgActivity;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author wulei
 * @Date 2018/4/9
 */

public class ReadMessageActivity extends BaseActivity implements View.OnClickListener, AualityTestAddPicAdapter.OnRecyclerViewClickListener, BuildFileAdapter.OnClickListener{
    @BindView(R.id.message_title)
    HXTextView messageTitle;
    @BindView(R.id.message_content)
    HXTextView messageContent;
    @BindView(R.id.message_time)
    HXTextView messageTime;
    @BindView(R.id.no_data_tv)
    HXTextView noDataTv;
    @BindView(R.id.pic_rv)
    RecyclerView picRv;
    @BindView(R.id.file_rv)
    RecyclerView fileRv;

    private Message message;
    private AualityTestAddPicAdapter aualityTestAddPicAdapter;
    private List<String> paths;
    private BuildFileAdapter fileAdapter;
    private List<FileBean> fileList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_read);
        ButterKnife.bind(this);
        message = (Message) getIntent().getExtras().get("message");
        if(message == null){
            return;
        }
        initView();
    }

    private void initView() {
        messageTitle.setText(message.getTitle());
        messageContent.setText(message.getContent());
        messageTime.setText(message.getCreateTime());

        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
        linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this);
        linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        if(message.getPicList().size() == 0 && message.getFileList().size() == 0){
            noDataTv.setVisibility(View.VISIBLE);
        }else {
            noDataTv.setVisibility(View.GONE);
            if(message.getFileList() != null && message.getFileList().size() > 0){
                fileRv.setLayoutManager(linearLayoutManager2);
                fileList = message.getFileList();
                fileAdapter = new BuildFileAdapter(this, fileList);
                fileAdapter.setOnClickListener(this);
                fileRv.setAdapter(fileAdapter);
            }
            if (message.getPicList() != null && message.getPicList().size() > 0) {
                picRv.setLayoutManager(linearLayoutManager1);
                paths = message.getPicList();
                aualityTestAddPicAdapter = new AualityTestAddPicAdapter(this, paths, this, false);
                picRv.setAdapter(aualityTestAddPicAdapter);
            }
        }
    }

    @OnClick({R.id.back_tv})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_tv:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(View v, int position) {
        TaskBigImgActivity.startActivity(ReadMessageActivity.this, (ArrayList<String>) paths, position);
    }

    @Override
    public void onClick(Object o) {
        FileBean fileBean = (FileBean) o;
        if(fileBean == null){
            return;
        }
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        String url = fileBean.getPath();
        intent.setData(Uri.parse(url));
        if (intent.resolveActivity(getPackageManager()) != null) {
            final ComponentName componentName = intent.resolveActivity(getPackageManager());
            // 打印Log   ComponentName到底是什么
            Log.e("wl", "componentName = " + componentName.getClassName());
            startActivity(Intent.createChooser(intent, "请选择浏览器"));
        } else {
            showToast("没有匹配的程序");
        }
    }
}
